﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.Globalization;
using ZO.SmartCore.Helpers;
using ZO.SmartCore.Xml;

#endregion

namespace ZO.SmartCore.Protocols.XMPP.Protocol.Common
{
    /// <summary>
    /// Encapsulates a presence element. The precense is used to notify other entities of availability, 
    /// or to subsribe/unsubsribe from their presence.
    /// </summary>
    public abstract class BasePresence : Stanza
    {

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="BasePresence"/> class.
        /// </summary>
        protected BasePresence() : base("presence") { } // Presence

        #endregion

        #region Destructor

        #endregion

        #region Fields

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>
        /// The possible type (currently) available to presence
        /// </summary>
        public PresenceType Type
        {
            get
            {
                return (PresenceType)this.GetAttributeAsEnum("type", typeof(PresenceType));
            }
            set
            {
                if (value == PresenceType.Available)
                {
                    this.RemoveAttribute("type");
                }
                else
                {
                    this.SetAttribute("type", ReflectionHelper.GetDescription(value));
                }
            }
        }

        /// <summary>
        /// specifies the particular availability status
        /// </summary>
        public ShowType Show
        {
            get
            {
                Element element1 = this["show"];

                if (element1 == null)
                {
                    return ShowType.None;
                }
                else
                {
                    switch (element1.Value)
                    {
                        case "away":
                            return ShowType.Away;

                        case "chat":
                            return ShowType.Chat;

                        case "dnd":
                            return ShowType.DontDisturb;

                        case "xa":
                            return ShowType.ExtendedAway;

                        default:
                            return ShowType.None;
                    }
                } // else
            }
            set
            {
                if (value != ShowType.None)
                {
                    this["show"] = new Element("show", ReflectionHelper.GetDescription(value));
                }
            }
        }

        /// <summary>
        /// used in conjunction with the show property to provide a detailed description of an availability state (e.g., "In a meeting")
        /// </summary>
        public string Status
        {
            get
            {
                Element element1 = this["status"];

                if (element1 == null)
                {
                    return element1.Value;
                }

                return "";
            }
            set
            {
                this["status"] = new Element("status", value);
            }
        }

        /// <summary>
        /// specifies the priority level of the resource. The value MUST be an integer between -128 and +127. 
        /// </summary>
        public int Priority
        {
            get
            {
                int value = 0;

                Element element1 = this["priority"];

                if (element1 == null)
                {
                    if (Int32.TryParse(element1.Value, out value))
                    {
                        return value;
                    }
                }

                return value;

            }
            set
            {
                if ((value < -128) && (value > 127))
                {
                    this["priority"] = new Element("priority", "0");
                }
                else
                {
                    this["priority"] = new Element("priority", value.ToString(CultureInfo.CurrentCulture));
                } // else
            }
        }
 

        #endregion

        #region Methods

        #endregion
    }
}
